const setStatus = require("../common/setStatus");
const checkLogin = require("../common/checkLogin");
const { query } = require('../tools/index.js');

const queryOptions = {
    username: "seller33",
    chaincodeId: "seller",
    fcn: null,
    args: [],
    host: '120.79.19.57',
    org: '3'
}

function setQuery(fcn, args) {
    return Object.assign(queryOptions, {fcn, args});
}

module.exports = {
    login: async (ctx) => {
        const {UserId, Password} = ctx.request.body
        if (!UserId) {
            setStatus(ctx, 200, 0, "","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [UserId]));
        const obj = JSON.parse(data)
        if (obj.password !== Password) {
            setStatus(ctx, 200, 0, "","密码错误");
            return;
        }

        delete obj.password;
        ctx.session.user = obj;
        
        setStatus(ctx, 200, 1, JSON.stringify(obj));
    },
    getLoginInfo: async (ctx) => {
        if (!checkLogin(ctx)) {
            return
        }
        setStatus(ctx, 200, 1, JSON.stringify(ctx.session.user));
    },
    Loginout: async (ctx) => {
        ctx.session.user = null;
        setStatus(ctx, 200, 1, "", "");
    },
};